<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>super关键字</title>
</head>
<body>
    <h1>super关键字</h1>
    <h2>super关键字指向当前对象的原型对象</h2>
    <h2>super关键字只能用在对象的方法之中</h2>
    <h2>super关键字表示原型对象时，只能用在对象的方法之中，用在其他地方都会报错</h2>

    <script>
        // 示例1：super作为对象使用
        const proto = {
            foo: 'hello'
        };

        const obj = {
            foo: 'world',
            find() {
                return super.foo;  // 返回原型对象的foo属性
            }
        };

        Object.setPrototypeOf(obj, proto);  // 设置obj的原型为proto
        console.log(obj.find());  // 输出: "hello"

        // 示例2：super在继承中的应用
        class Animal {
            constructor(name) {
                this.name = name;
            }
            speak() {
                return `${this.name} makes a sound`;
            }
        }

        class Dog extends Animal {
            speak() {
                return super.speak() + ', woof!';  // 调用父类的speak方法
            }
        }

        const dog = new Dog('Rex');
        console.log(dog.speak());  // 输出: "Rex makes a sound, woof!"

        // 示例3：在对象字面量中使用super
        const parent = {
            greet() {
                return 'Hello from parent';
            }
        };

        const child = {
            greet() {
                return super.greet() + ' and child';  // 调用原型对象的方法
            }
        };

        Object.setPrototypeOf(child, parent);
        console.log(child.greet());  // 输出: "Hello from parent and child"
    </script>
</body>
</html>
